package com.lanou.controller;

import com.google.gson.Gson;
import com.lanou.pojo.Book;
import com.lanou.service.SelectService;
import com.lanou.utils.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "BookAndUserServlet",urlPatterns = "/bookanduser")
public class BookAndUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    private SelectService selectService = new SelectService();
    //在user界面显示单个图书
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bid = Integer.parseInt(request.getParameter("bid"));
        HttpSession session = request.getSession();
        String stringuid = String.valueOf(session.getAttribute("id"));
        response.setContentType("application/json;charset=utf-8");
        AjaxResult ar = new AjaxResult();
        PrintWriter out = response.getWriter();
        String username = null;
        if ("null".equals(stringuid)){

        }else {
            int uid = Integer.parseInt(stringuid);
            username = selectService.selectUsernameByid(uid);
        }
        Book book = selectService.selectBookBybid(bid);
        ar.setUsername(username);
        if (book!=null){
            ar.setData(book);
            ar.setCode(1000);
            ar.setMsg("查询成功");
        }else {
            ar.setData(1001);
            ar.setMsg("查询失败");
        }
        out.write(new Gson().toJson(ar));
        out.close();
    }
}
